#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Observable Model """
__author__ = "Lei Chen <hide1713@gmail.com>"
import logging
import operator

class Model(object):
    """A MVC model class. This class stored all the cog data sets
    for all users.Each data set can be addressed by its file name
    """
    
    def __init__(self, ):
        """
        """
        self.log = logging.getLogger(self.__class__.__name__)
        self._data_set = set()
        self._listener_list = []
        self.is_data_changed = False
    
    def add_data(self, data):
        """Add data to data set. the data should be a CogModel object to represent
        a file in data directory
        
        Arguments:
        - `data`: a CogModel
        """
        self.is_data_changed = True
        self.log.info("Load data :"+data.base_name)
        self._data_set.add(data)

    def remove_data(self, file_name,user_id):
        """Remove data file matchs file_name and user_id
        """
        model = None
        for data in self._data_set:
            if data.base_name == file_name and data.owner == user_id:
                model = data
                self.log.info("Remove data :"+data.base_name)
                break
        if model:
            self._data_set.remove(model)
            del model

#    def get_data(self):
#        """return the data set"""
#        return self._data_set

    def get_child_model_names(self,user_id):
        """Return a list of file name string
        """
        name_list = []
        for data in self._data_set:
            if data.owner == user_id:
                name_list.append(data.base_name)
        return name_list
        
    def get_cog(self, cog, file_name, user_id):
        """Get a single Cog object from given file_name and user_id
        
        Arguments:
        - `cog`: cog family name
        - `file_name`: a file base name
        - `user_id`: user id
        """
        for data in self._data_set:
            if data.base_name == file_name and data.owner == user_id:
                return data.get_single_cog(cog)
        self.log.error("Can not get data from " + cog + ":" + file_name)
        raise KeyError("Can not get data from " + cog + ":" + file_name)

    def get_child_model(self, file_name,user_id):
        """Return a cog_model object
        
        Arguments:
        - `file_name`: user file name
        - `user_id`: user id
        """
        for data in self._data_set:
            if data.base_name == file_name and data.owner == user_id:
                return data
        self.log.error("Can not get model:" + file_name)
        raise KeyError("Can not get model:" + file_name)
            

#    def get_cog_list(self, ):
#        """Return (name, file_name, read) for all data set
#        """
#        new_cog_list = []
#        for mod in self._data_set:
#            new_cog_list += mod.get_non_empty_cog_names()
#        return sorted(new_cog_list, key = operator.itemgetter(0))
        

        

